﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using LiveChartsCore;
using LiveChartsCore.SkiaSharpView;
using LiveChartsCore.SkiaSharpView.Drawing;
using LiveChartsCore.SkiaSharpView.Painting;
using LiveChartsCore.SkiaSharpView.WPF;
using SkiaSharp;

namespace TestWpfLiveCharts2.Controls
{
    /// <summary>
    /// 用于处理表格中一段矩形选择区
    /// </summary>
    public class SelectRectangleSectionRack : IDisposable
    {
        public SelectRectangleSectionRack(CartesianChart chartControl, double startX, double startY)
        {
            this.chartControl = chartControl ?? throw new ArgumentNullException(nameof(chartControl));
            rectSection = new RectangularSection
            {
                Fill = new SolidColorPaint(new SKColor(187, 222, 251)),
                Xi = startX,
                Xj = startX,
                Yi = startY,
                Yj = startY,
            };
            // 确保图表中可放置矩形区
            if (chartControl.Sections is not ICollection<Section<SkiaSharpDrawingContext>>)
            {
                chartControl.Sections = new ObservableCollection<Section<SkiaSharpDrawingContext>>();
            }
            var chartSections = (ICollection<Section<SkiaSharpDrawingContext>>)chartControl.Sections;
            chartSections.Add(rectSection);
        }

        private readonly CartesianChart chartControl;
        private readonly RectangularSection rectSection;

        /// <summary>
        /// 矩形更新过，才有真实大小，这表示它可见且可用。
        /// </summary>
        public bool IsValidRect => rectSection.IsVisible && rectSection.Xi != rectSection.Xj && rectSection.Yi != rectSection.Yj;

        /// <summary>
        /// 根据结束坐标，更新矩形区
        /// </summary>
        public void UpdateRect(double endX, double endY)
        {
            rectSection.Xj = endX;
            rectSection.Yj = endY;
        }

        #region IDisposable

        private bool disposedValue;

        protected virtual void Dispose(bool disposing)
        {
            if (!disposedValue)
            {
                if (disposing)
                {
                    // TODO: 释放托管状态(托管对象)
                    if (chartControl.Sections is ICollection<Section<SkiaSharpDrawingContext>> sections)
                    {
                        sections.Remove(rectSection);
                    }
                }

                // TODO: 释放未托管的资源(未托管的对象)并重写终结器
                // TODO: 将大型字段设置为 null
                disposedValue = true;
            }
        }

        // // TODO: 仅当“Dispose(bool disposing)”拥有用于释放未托管资源的代码时才替代终结器
        // ~SelectRectangleSectionRack()
        // {
        //     // 不要更改此代码。请将清理代码放入“Dispose(bool disposing)”方法中
        //     Dispose(disposing: false);
        // }

        public void Dispose()
        {
            // 不要更改此代码。请将清理代码放入“Dispose(bool disposing)”方法中
            Dispose(disposing: true);
            GC.SuppressFinalize(this);
        }

        #endregion IDisposable
    }
}